package com.xbongbong.saas.help;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.constant.TimeConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.elasticsearch.util.EsHelper;
import com.xbongbong.paas.elasticsearch.util.EsUtil;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.help.subform.TransferSubFormHelper;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.service.CompanyConfigService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.Arith;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.businessdata.pojo.dto.ProductListDataDTO;
import com.xbongbong.pro.companyconfig.pojo.dto.CompanyConfigHasBeenEnableDTO;
import com.xbongbong.pro.companyconfig.pojo.vo.CompanyConfigHasBeenEnableVO;
import com.xbongbong.pro.constant.BusinessConstant;
import com.xbongbong.pro.constant.CommonConstant;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.ProductStockEntity;
import com.xbongbong.saas.domain.entity.ProductWarehouseEntity;
import com.xbongbong.saas.domain.entity.SupplierProductEntity;
import com.xbongbong.saas.domain.entity.ext.WarehouseEntityExt;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.saas.enums.GuaranteeStatusEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.enums.business.WarehouseEnum;
import com.xbongbong.saas.enums.subform.ProductStockSubFormEnum;
import com.xbongbong.saas.enums.subform.ProductWarehouseSubFormEnum;
import com.xbongbong.saas.model.ProductCategoryModel;
import com.xbongbong.saas.model.WarehouseModel;
import com.xbongbong.saas.service.ProductService;
import com.xbongbong.saas.service.UserTeamService;
import com.xbongbong.saas.service.WarehouseService;
import com.xbongbong.saas.service.toolbox.help.AttrDecimalPrecisionHelper;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static com.xbongbong.saas.enums.business.ProductEnum.PRODUCT_NO;
import static com.xbongbong.saas.toolbox.help.FastJsonHelper.getDoubleOrDefaultFromFormData;
import static com.xbongbong.saas.toolbox.help.FastJsonHelper.getIntegerOrDefaultFromFormData;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.existsQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * 产品列表帮助类
 * 用于失智需求的渲染查询等
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/6/1 15:25
 * @since v1.0
 */
@Component
public class ProductListHelp {

    public static final Logger LOG = LoggerFactory.getLogger(ProductListHelp.class);

    @Resource
    private EsHelper esHelper;
    @Resource
    private ProductService productService;
    @Resource
    private ProductCategoryModel productCategoryModel;
    @Resource
    private CompanyConfigService companyConfigService;
    @Resource
    private AttrDecimalPrecisionHelper attrDecimalPrecisionHelper;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private UserTeamService userTeamService;
    @Resource
    private TransferSubFormHelper transferSubFormHelper;
    @Resource
    private WarehouseModel warehouseModel;

    public Map<Long, JSONArray> childProductList(FormDataListDTO formDataListDTO, List<Long> dataIdList, String corpid, Map<String, FieldAttrEntity> productExplainMap) throws XbbException {
        // 协同团队
        Map<Long, List<String>> mainUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, List<String>> coUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        // 成本字段解释
        FieldAttrEntity costField = new FieldAttrEntity();
        FieldAttrEntity priceField = new FieldAttrEntity();
        FieldAttrEntity stockField = new FieldAttrEntity();
        List<FieldAttrEntity> fieldAttrEntityList = new ArrayList<>();
        try {
            userTeamService.getUserIdMap(dataIdList, corpid, XbbRefTypeEnum.PRODUCT.getCode(), false, mainUserMap, coUserMap);
            costField = productExplainMap.getOrDefault(ProductEnum.COST.getAttr(), new FieldAttrEntity());
            priceField = productExplainMap.getOrDefault(ProductEnum.PRICE.getAttr(), new FieldAttrEntity());
            stockField = productExplainMap.getOrDefault(ProductEnum.STOCK.getAttr(), new FieldAttrEntity());
            fieldAttrEntityList = new ArrayList<>(productExplainMap.values());
        } catch (Exception e) {
            LOG.info("ProductListHelp.childProductList", e);
        }


        Integer priceAccuracy;
        try {
            priceAccuracy = productExplainMap.get(ProductEnum.PRICE.getAttr()).getAccuracy();
        } catch (Exception e) {
            priceAccuracy = AttrDecimalPrecisionHelper.DEFAULT_PRECISION;
        }
        Integer costAccuracy;
        try {
            costAccuracy = productExplainMap.get(ProductEnum.COST.getAttr()).getAccuracy();
        } catch (Exception e) {
            costAccuracy = AttrDecimalPrecisionHelper.DEFAULT_PRECISION;
        }
        Integer stockAccuracy;
        try {
            stockAccuracy = productExplainMap.get(ProductEnum.STOCK.getAttr()).getAccuracy();
        } catch (Exception e) {
            stockAccuracy = AttrDecimalPrecisionHelper.DEFAULT_PRECISION;
        }

        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword",corpid));
        boolQueryBuilder.filter(termQuery(StringConstant.DEL,DelEnum.NORMAL.getDel()));
        boolQueryBuilder.filter(termsQuery(ProductEnum.getEsAttr4Keyword(ProductEnum.PARENT_ID),dataIdList));
//        boolQueryBuilder.filter(termQuery(ProductEnum.getEsAttr4Keyword(ProductEnum.SPECIFICATION),1));
        // 限制返回值
        List<String> fieldList = Arrays.asList(FieldTypeEnum.DATAID.getAlias(), StringConstant.JSON_DATA,PRODUCT_NO.getAttr());
        List<PaasFormDataEntityExt> paasFormDataEntityExtList = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_PRODUCT,boolQueryBuilder,PaasFormDataEntityExt.class,fieldList);
        Map<Long,JSONArray> productMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        productService.setChildSpecification(paasFormDataEntityExtList,corpid);
        for (PaasFormDataEntityExt entityExt : paasFormDataEntityExtList){
            JSONObject data = entityExt.getData();
            Long parentId = data.getLong(ProductEnum.PARENT_ID.getAttr());

            boolean isCostFieldNeedHide = false;
            boolean isPriceFieldNeedHide = false;
            boolean isStockFieldNeedHide = false;
            try {
                HandlerExplainDTO handlerExplainDTO = new HandlerExplainDTO();
                BeanUtil.copyProperties(formDataListDTO, handlerExplainDTO);
                handlerExplainDTO.setCreatorId(entityExt.getCreatorId());
                handlerExplainDTO.setOwnerId(mainUserMap.getOrDefault(parentId, new ArrayList<>()));
                handlerExplainDTO.setCoUserId(coUserMap.getOrDefault(parentId, new ArrayList<>()));
                handlerExplainDTO.setExplainList(fieldAttrEntityList);
                isCostFieldNeedHide = ExplainUtil.isFieldNeedHide(costField, handlerExplainDTO);
                isPriceFieldNeedHide = ExplainUtil.isFieldNeedHide(priceField, handlerExplainDTO);
                isStockFieldNeedHide = ExplainUtil.isFieldNeedHide(stockField, handlerExplainDTO);
            } catch (Exception e) {
                LOG.info("ProductListHelp.childProductList", e);
            }

            JSONArray childProductArray = productMap.getOrDefault(parentId,new JSONArray());
            JSONObject childProduct = new JSONObject();
            childProduct.put("barcode",data.getString(ProductEnum.BARCODE.getAttr()));
            childProduct.put("cost", Arith.round(StringUtil.toDouble(data.getString(ProductEnum.COST.getAttr()), 0D), costAccuracy));
            childProduct.put("price", Arith.round(StringUtil.toDouble(data.getString(ProductEnum.PRICE.getAttr()), 0D), priceAccuracy));
            childProduct.put("productSpecNo",data.getString(ProductEnum.PRODUCT_NO.getAttr()));
            childProduct.put("specification",data.getString(ProductEnum.SPECIFICATION.getAttr()));
            childProduct.put("specificationLinkText",data.getString(ProductEnum.SPECIFICATION_LINK_TEXT.getAttr()));
            childProduct.put("stock",data.getString(ProductEnum.STOCK.getAttr()));
            childProduct.put("stock4Show", Arith.round(StringUtil.toDouble(data.getString(ProductEnum.STOCK.getAttr()), 0D), stockAccuracy));
            if (isStockFieldNeedHide) {
                childProduct.put("stock4Show", StringConstant.INVISIBLE_VALUE);
            }
            childProduct.put("productId",entityExt.getDataId());
            childProduct.put("cost4Show", Arith.round(StringUtil.toDouble(data.getString(ProductEnum.COST.getAttr()), 0D), costAccuracy));
            if (isCostFieldNeedHide) {
                childProduct.put("cost4Show", StringConstant.INVISIBLE_VALUE);
            }
            childProduct.put("price4Show", Arith.round(StringUtil.toDouble(data.getString(ProductEnum.PRICE.getAttr()), 0D), priceAccuracy));
            if (isPriceFieldNeedHide) {
                childProduct.put("price4Show", StringConstant.INVISIBLE_VALUE);
            }
            childProductArray.add(childProduct);
            productMap.put(parentId,childProductArray);
        }
        return productMap;
    }

    public Map<Long, JSONArray> getStockSearchChildProduct(List<Long> dataIdList, String corpid, Map<String, FieldAttrEntity> productExplainMap, List<Long> warehouseIdList) throws XbbException {
        Integer costAccuracy;
        try {
            costAccuracy = productExplainMap.get(ProductEnum.COST.getAttr()).getAccuracy();
        } catch (Exception e) {
            costAccuracy = AttrDecimalPrecisionHelper.DEFAULT_PRECISION;
        }
        Map<Long,JSONArray> productMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (!warehouseIdList.isEmpty()){
            BoolQueryBuilder boolQueryBuilder = boolQuery();
            boolQueryBuilder.filter(termQuery("corpid.keyword",corpid));
            boolQueryBuilder.filter(termQuery(StringConstant.DEL,DelEnum.NORMAL.getDel()));
            boolQueryBuilder.filter(termsQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.WAREHOUSE_ID),warehouseIdList));
            boolQueryBuilder.filter(termQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.WAREHOUSE_CHECKED),1));
            boolQueryBuilder.filter(termsQuery("id", dataIdList));
            List<PaasFormDataEntity> productWarehouseEntityList = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE,boolQueryBuilder, PaasFormDataEntity.class,Arrays.asList(ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.PRODUCT_ID), ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.NUM)));
            List<ProductWarehouseEntity> productWarehouseEntities = transferSubFormHelper.transferFormDataToProductWarehouseList(productWarehouseEntityList);
            for (ProductWarehouseEntity entityExt : productWarehouseEntities){
                JSONObject singleWarehouse = new JSONObject();
                Long productId = entityExt.getProductId();
                JSONArray childProductArray = productMap.getOrDefault(productId,new JSONArray());
                singleWarehouse.put("warehouseName", entityExt.getWarehouseName());
                singleWarehouse.put("cost", Arith.round(entityExt.getCost(), costAccuracy));
                singleWarehouse.put("stock", entityExt.getNum());
                singleWarehouse.put("productId",productId);
                childProductArray.add(singleWarehouse);
                productMap.put(productId, childProductArray);
            }
        }
        return productMap;
    }

    public Map<Long,JSONArray> warningInfo(List<Long> dataIdList, String corpid, FormDataListDTO formDataListDTO, PaasFormDataEsListVO formDataEsListVO,Map<Long,String> productSearchMap) throws XbbException {
        Map<Long,JSONArray> productMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        //全仓预警sku
        Set<Long> allWarehouseSet = new HashSet<>();
        //分仓预警
        Map<Long,Integer> warehouseNumMap = new HashMap<>();
        Long warehouseId = 0L;
        Integer warningType = 0;
        if (formDataListDTO.getCommonFilter() != null) {
            warehouseId = formDataListDTO.getCommonFilter().getLong("warehouseId");
            warningType = formDataListDTO.getCommonFilter().getInteger("warningType");
        }
        List<PaasFormDataEntityExt> paasFormDataEntityExtList = formDataEsListVO.getPaasFormDataESList();
        Boolean isAllWarehouse = Objects.isNull(warehouseId) || Objects.equals(warehouseId,0L);
        if (isAllWarehouse){
            for (PaasFormDataEntityExt entityExt : paasFormDataEntityExtList){
                JSONObject data = entityExt.getData();
                Long dataId = entityExt.getDataId();
                Integer isNotify = getIntegerOrDefaultFromFormData(data, ProductEnum.IS_NOTIFY.getAttr(), 0);
                Double stock = getDoubleOrDefaultFromFormData(data, ProductEnum.STOCK.getAttr(), 0D);
                Double stockUpperLimit = getDoubleOrDefaultFromFormData(data, ProductEnum.STOCK_UPPER_LIMIT.getAttr(), 0D);
                Double stockLowerLimit = getDoubleOrDefaultFromFormData(data, ProductEnum.STOCK_LOWER_LIMIT.getAttr(), 0D);
                boolean lowerWarning = false;
                boolean upperWarning = false;
                String warnType = "";
                if (isNotify == 1 && stock <= stockLowerLimit) {
                    warnType = I18nMessageUtil.getMessage(BusinessConstant.WARNING_LOWER);
                    lowerWarning = true;
                } else if (isNotify == 1 && stock >= stockUpperLimit) {
                    warnType = I18nMessageUtil.getMessage(BusinessConstant.WARNING_UPPER);
                    upperWarning = true;
                }
                if (!lowerWarning && !upperWarning){
                    continue;
                }
                JSONArray productArray = productMap.getOrDefault(dataId,new JSONArray());
                JSONObject product = new JSONObject();
                product.put("stock",data.getDouble(ProductEnum.STOCK.getAttr()));
                product.put("stockLowerLimit",data.getDouble(ProductEnum.STOCK_LOWER_LIMIT.getAttr()));
                product.put("stockUpperLimit",data.getDouble(ProductEnum.STOCK_UPPER_LIMIT.getAttr()));
                product.put("warehouseName", I18nMessageUtil.getMessage(I18nStringConstant.MAIN_WAREHOUSE));
                product.put("warning",true);
                product.put("warningType",warnType);
                productArray.add(product);
                productMap.put(dataId,productArray);
                allWarehouseSet.add(dataId);
            }
        }
        List<Object> warehouseIdList = new ArrayList<>();
        for (ConditionsEntityExt conditionsEntityExt : formDataListDTO.getConditions()){
            if (Objects.equals(conditionsEntityExt.getFieldType(),FieldTypeEnum.STOCK_WAREHOUSE_ID.getType())){
                warehouseIdList = conditionsEntityExt.getValue();
                break;
            }
        }
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword",corpid));
        boolQueryBuilder.filter(termQuery(StringConstant.DEL,DelEnum.NORMAL.getDel()));
        boolQueryBuilder.filter(termsQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.PRODUCT_ID),dataIdList));
        boolQueryBuilder.filter(termQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.WAREHOUSE_CHECKED), 1));
        if (!warehouseIdList.isEmpty()){
            boolQueryBuilder.filter(termsQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.WAREHOUSE_ID),warehouseIdList));
        }
        List<String> fieldList = Arrays.asList(ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.PRODUCT_ID), ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.IS_NOTIFY), ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.NUM), ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.STOCK_LOWER_LIMIT), ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.WAREHOUSE_NAME));
        List<PaasFormDataEntity> productWarehouseEntityList = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE,boolQueryBuilder,PaasFormDataEntity.class,fieldList);
        List<ProductWarehouseEntity> productWarehouseEntities = transferSubFormHelper.transferFormDataToProductWarehouseList(productWarehouseEntityList);
        for (ProductWarehouseEntity productWarehouseEntity : productWarehouseEntities){
            Long dataId = productWarehouseEntity.getProductId();
            boolean lowerWarning = false;
            boolean upperWarning = false;
            String warnType = "";
            if (Objects.isNull(productWarehouseEntity.getIsNotify()) || Objects.isNull(productWarehouseEntity.getNum())
                || Objects.isNull(productWarehouseEntity.getStockLowerLimit()) || Objects.isNull(productWarehouseEntity.getStockUpperLimit())) {
                continue;
            }
            if (productWarehouseEntity.getIsNotify() == 1 && productWarehouseEntity.getNum() <= productWarehouseEntity.getStockLowerLimit()) {
                warnType = I18nMessageUtil.getMessage(BusinessConstant.WARNING_LOWER);
                lowerWarning = true;
            } else if (productWarehouseEntity.getIsNotify() == 1 && productWarehouseEntity.getNum() >= productWarehouseEntity.getStockUpperLimit()) {
                warnType = I18nMessageUtil.getMessage(BusinessConstant.WARNING_UPPER);
                upperWarning = true;
            }
            if (!lowerWarning && !upperWarning){
                continue;
            }
            JSONArray productArray = productMap.getOrDefault(dataId,new JSONArray());
            JSONObject product = new JSONObject();
            product.put("stock",productWarehouseEntity.getNum());
            product.put("stockLowerLimit",productWarehouseEntity.getStockLowerLimit());
            product.put("stockUpperLimit",productWarehouseEntity.getStockUpperLimit());
            product.put("warehouseName",productWarehouseEntity.getWarehouseName());
            product.put("warning",true);
            product.put("warningType",warnType);
            productArray.add(product);
            productMap.put(dataId,productArray);
            Integer num = warehouseNumMap.getOrDefault(dataId,0);
            num++;
            warehouseNumMap.put(dataId,num);
        }
        for (Long dataId : dataIdList){
            String str = "";
            if (allWarehouseSet.contains(dataId)){
                str = I18nMessageUtil.getMessage(I18nStringConstant.FULL_WAREHOUSE_WARNING);
            }
            if (warehouseNumMap.containsKey(dataId)){
                if (str.isEmpty()){
                    str = warehouseNumMap.get(dataId) + I18nMessageUtil.getMessage(I18nStringConstant.WAREHOUSE_ALERT);
                }else {
                    str += I18nMessageUtil.getMessage(CommonConstant.AND) + warehouseNumMap.get(dataId) + I18nMessageUtil.getMessage(I18nStringConstant.WAREHOUSE_ALERT);
                }
            }
            productSearchMap.put(dataId,str);
        }
        return productMap;
    }

    public Map<Long,JSONArray> batchInfo(List<Long> dataIdList, String corpid, FormDataListDTO formDataListDTO, PaasFormDataEsListVO formDataEsListVO, Map<Long, String> productSearchMap) throws XbbException {
        Map<Long,JSONArray> productMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        List<Object> warehouseIdList = new ArrayList<>();
        for (ConditionsEntityExt conditionsEntityExt : formDataListDTO.getConditions()){
            if (Objects.equals(conditionsEntityExt.getAttr(),ProductEnum.getAttrConnectData(ProductEnum.WAREHOUSE))){
                warehouseIdList = conditionsEntityExt.getValue();
                break;
            }
        }
        Map<Long,String> creatorMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        formDataEsListVO.getPaasFormDataESList().forEach(item -> creatorMap.put(item.getDataId(),item.getCreatorId()));
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword",corpid));
        boolQueryBuilder.filter(termQuery(StringConstant.DEL,DelEnum.NORMAL.getDel()));
        boolQueryBuilder.mustNot(termQuery(ProductStockSubFormEnum.getEsAttr(ProductStockSubFormEnum.GUARANTEE_PERIOD),0));
        boolQueryBuilder.mustNot(termQuery(ProductStockSubFormEnum.getEsAttr(ProductStockSubFormEnum.PRODUCE_DATE),0));
        boolQueryBuilder.must(existsQuery(ProductStockSubFormEnum.getEsAttr(ProductStockSubFormEnum.GUARANTEE_PERIOD)));
        boolQueryBuilder.must(existsQuery(ProductStockSubFormEnum.getEsAttr(ProductStockSubFormEnum.PRODUCE_DATE)));
        boolQueryBuilder.filter(termQuery(ProductStockSubFormEnum.getEsAttr(ProductStockSubFormEnum.WAREHOUSE_CHECKED),1));
        boolQueryBuilder.filter(termsQuery(ProductStockSubFormEnum.getEsAttr(ProductStockSubFormEnum.PRODUCT_ID),dataIdList));
        if (!warehouseIdList.isEmpty()){
            boolQueryBuilder.filter(termsQuery(ProductStockSubFormEnum.getEsAttr(ProductStockSubFormEnum.WAREHOUSE_ID),warehouseIdList));
        }
        Integer guaranteeStatus = formDataListDTO.getCommonFilter().getInteger("guaranteeStatus");
        guaranteeStatus = guaranteeStatus == null ? 0 : guaranteeStatus;
        GuaranteeStatusEnum guaranteeStatusEnum = GuaranteeStatusEnum.getByCode(guaranteeStatus);
        EsUtil.conditionGuarantee(boolQueryBuilder,guaranteeStatusEnum.getSymbol(),Collections.singletonList(guaranteeStatusEnum.getTime()));
        // 限制返回值
        List<String> fieldList = Arrays.asList(ProductStockSubFormEnum.getAttrConnectData(ProductStockSubFormEnum.PRODUCT_ID), ProductStockSubFormEnum.getAttrConnectData(ProductStockSubFormEnum.BATCH), ProductStockSubFormEnum.getAttrConnectData(ProductStockSubFormEnum.GUARANTEE_PERIOD), ProductStockSubFormEnum.getAttrConnectData(ProductStockSubFormEnum.PRODUCE_DATE), ProductStockSubFormEnum.getAttrConnectData(ProductStockSubFormEnum.NUM), ProductStockSubFormEnum.getAttrConnectData(ProductStockSubFormEnum.WAREHOUSE_NAME), ProductStockSubFormEnum.getAttrConnectData(ProductStockSubFormEnum.COST), ProductStockSubFormEnum.getAttrConnectData(ProductStockSubFormEnum.PARENT_ID));
        List<PaasFormDataEntity> productStockEntityList = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_PRODUCT_STOCK_INFO,boolQueryBuilder,PaasFormDataEntity.class,fieldList);
        List<ProductStockEntity> productStockEntities = transferSubFormHelper.transferFormDataToProductStockList(productStockEntityList);

        Long now = DateTimeUtil.getInt();
        Map<Long, List<String>> mainUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, List<String>> coUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        userTeamService.getUserIdMap(dataIdList, formDataListDTO.getCorpid(), XbbRefTypeEnum.PRODUCT.getCode(), false, mainUserMap, coUserMap);
        FieldAttrEntity stockField = formDataListDTO.getExplainMap().get(ProductEnum.STOCK.getAttr());
        FieldAttrEntity costField = formDataListDTO.getExplainMap().get(ProductEnum.COST.getAttr());
        boolean isStockFieldNeedHide = true;
        boolean isCostFieldNeedHide = true;
        Integer stockAccuracy = stockField.getAccuracy();
        Integer costAccuracy = costField.getAccuracy();
        Map<Long,Boolean> isStockFieldNeedHideMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long,Boolean> isCostFieldNeedHideMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (ProductStockEntity productStockEntity : productStockEntities){
            if (!Objects.isNull(stockField)){
                if (isStockFieldNeedHideMap.containsKey(productStockEntity.getParentId())){
                    isStockFieldNeedHide = isStockFieldNeedHideMap.get(productStockEntity.getParentId());
                }else {
                    HandlerExplainDTO handlerExplainDTO = new HandlerExplainDTO();
                    BeanUtil.copyProperties(formDataListDTO, handlerExplainDTO);
                    handlerExplainDTO.setCreatorId(creatorMap.getOrDefault(productStockEntity.getProductId(), "0"));
                    handlerExplainDTO.setOwnerId(mainUserMap.getOrDefault(productStockEntity.getParentId(), new ArrayList<>()));
                    handlerExplainDTO.setCoUserId(coUserMap.getOrDefault(productStockEntity.getParentId(), new ArrayList<>()));
                    handlerExplainDTO.setExplainList(new ArrayList<>(formDataListDTO.getExplainMap().values()));
                    isStockFieldNeedHide = ExplainUtil.isFieldNeedHide(stockField, handlerExplainDTO) || Objects.equals(stockField.getIsOpen(), 0);
                    isStockFieldNeedHideMap.put(productStockEntity.getParentId(), isStockFieldNeedHide);
                }
            }
            if (!Objects.isNull(costField)){
                if (isCostFieldNeedHideMap.containsKey(productStockEntity.getParentId())){
                    isCostFieldNeedHide = isCostFieldNeedHideMap.get(productStockEntity.getParentId());
                }else {
                    HandlerExplainDTO handlerExplainDTO = new HandlerExplainDTO();
                    BeanUtil.copyProperties(formDataListDTO, handlerExplainDTO);
                    handlerExplainDTO.setCreatorId(creatorMap.getOrDefault(productStockEntity.getProductId(), "0"));
                    handlerExplainDTO.setOwnerId(mainUserMap.getOrDefault(productStockEntity.getParentId(), new ArrayList<>()));
                    handlerExplainDTO.setCoUserId(coUserMap.getOrDefault(productStockEntity.getParentId(), new ArrayList<>()));
                    handlerExplainDTO.setExplainList(new ArrayList<>(formDataListDTO.getExplainMap().values()));
                    isCostFieldNeedHide = ExplainUtil.isFieldNeedHide(costField, handlerExplainDTO) || Objects.equals(costField.getIsOpen(), 0);
                    isCostFieldNeedHideMap.put(productStockEntity.getParentId(), isCostFieldNeedHide);
                }
            }
            Long dataId = productStockEntity.getProductId();
            JSONArray productArray = productMap.getOrDefault(dataId,new JSONArray());
            JSONObject product = new JSONObject();
            product.put("batch",productStockEntity.getBatch());
            product.put("num", productStockEntity.getNum());
            Long guaranteePeriod = productStockEntity.getGuaranteePeriod();
            guaranteePeriod = guaranteePeriod == null ? 0L : guaranteePeriod;
            Long produceDate = productStockEntity.getProduceDate();
            produceDate = produceDate == null ? 0L : produceDate;
            product.put("guaranteePeriod",guaranteePeriod / TimeConstant.SECONDS_PER_DAY);
            product.put("guaranteeStatus",getGuaranteePeriod(guaranteePeriod,produceDate,now));
            if (isStockFieldNeedHide){
                product.put("stock","--");
            }else {
                product.put("stock",attrDecimalPrecisionHelper.setDecPrecision(productStockEntity.getNum(), stockAccuracy));
            }
            if (isCostFieldNeedHide){
                product.put("cost","--");
                product.put("costTotal","--");
            }else{
                product.put("cost",attrDecimalPrecisionHelper.setDecPrecision(productStockEntity.getCost(), costAccuracy));
                product.put("costTotal",attrDecimalPrecisionHelper.setDecPrecision(Arith.mul(productStockEntity.getNum(), Objects.isNull(productStockEntity.getCost()) ? 0D : productStockEntity.getCost()), costAccuracy));
            }
            if (Objects.equals(produceDate,0L)){
                product.put("produceDate","");
            }else {
                product.put("produceDate",DateTimeUtil.getStringEpochSecond(produceDate,DateTimeUtil.SDFDate));
            }
            product.put("warehouseName",productStockEntity.getWarehouseName());
            productArray.add(product);
            productMap.put(dataId,productArray);
            productSearchMap.put(dataId,getGuaranteePeriod(guaranteePeriod,produceDate,now));
        }
        return productMap;
    }

    private String getGuaranteePeriod(Long guaranteePeriod, Long produceDate, Long now) {
        Long expirationDate = guaranteePeriod + produceDate;
        if (expirationDate < now){
            return GuaranteeStatusEnum.OUT_OF_DATE.getName() + ((now - expirationDate) / TimeConstant.SECONDS_PER_DAY ) + I18nMessageUtil.getMessage(CommonConstant.DAY);
        }else {
            return I18nMessageUtil.getMessage(I18nStringConstant.REMAINDER) + String.valueOf((expirationDate - now) / TimeConstant.SECONDS_PER_DAY + 1) + I18nMessageUtil.getMessage(CommonConstant.DAY);
        }
    }

    private String getGuaranteePeriodShow(Long guaranteePeriod, Long produceDate, Long now) {
        Long expirationDate = guaranteePeriod + produceDate;
        if (expirationDate < now){
            return GuaranteeStatusEnum.OUT_OF_DATE.getName() + ((now - expirationDate) / TimeConstant.SECONDS_PER_DAY ) + I18nMessageUtil.getMessage(CommonConstant.DAY);
        }else {
            return I18nMessageUtil.getMessage(I18nStringConstant.REMAINDER) + String.valueOf((expirationDate - now) / TimeConstant.SECONDS_PER_DAY + 1) + I18nMessageUtil.getMessage(CommonConstant.DAY);
        }
    }

    public Map<Long,JSONArray> productWarehouseList(FormDataListDTO formDataListDTO, List<PaasFormDataEntityExt>  paasFormDataESList, List<Long> dataIdList, String corpid, List<Long> warehouseIdList, Map<String, FieldAttrEntity> productExplainMap) throws XbbException {
        Integer costAccuracy;
        try {
            costAccuracy = productExplainMap.get(ProductEnum.COST.getAttr()).getAccuracy();
        } catch (Exception e) {
            costAccuracy = AttrDecimalPrecisionHelper.DEFAULT_PRECISION;
        }
        // 协同团队
        Map<Long, List<String>> mainUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, List<String>> coUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        // 成本字段解释
        FieldAttrEntity costField = null;
        FieldAttrEntity stockField = null;
        List<FieldAttrEntity> fieldAttrEntityList = new ArrayList<>();
        try {
            userTeamService.getUserIdMap(dataIdList, corpid, XbbRefTypeEnum.PRODUCT.getCode(), false, mainUserMap, coUserMap);
            costField = productExplainMap.getOrDefault(ProductEnum.COST.getAttr(), new FieldAttrEntity());
            stockField = productExplainMap.getOrDefault(ProductEnum.STOCK.getAttr(), new FieldAttrEntity());
            fieldAttrEntityList = new ArrayList<>(productExplainMap.values());
        } catch (Exception e) {
            LOG.info("ProductListHelp.childProductList", e);
        }
        CompanyConfigHasBeenEnableDTO companyConfigHasBeenEnableDTO = new CompanyConfigHasBeenEnableDTO(CompanyConfigEnum.ZERO_STOCK.getAlias());
        companyConfigHasBeenEnableDTO.setCorpid(corpid);
        CompanyConfigHasBeenEnableVO companyConfigHasBeenEnableVO = companyConfigService.hasBeenEnable(companyConfigHasBeenEnableDTO);
        boolean filterZeroStock = Objects.equals(companyConfigHasBeenEnableVO.getEnable(),1);
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword",corpid));
        boolQueryBuilder.filter(termsQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.PRODUCT_ID),dataIdList));
        boolQueryBuilder.filter(termQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.WAREHOUSE_CHECKED), 1));
        boolQueryBuilder.filter(termQuery("del",0));
        if (!warehouseIdList.isEmpty()) {
            boolQueryBuilder.filter(termsQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.WAREHOUSE_ID),warehouseIdList));
        }
        if (filterZeroStock){
            boolQueryBuilder.mustNot(termQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.NUM), 0));
        }
        Map<Long, PaasFormDataEntityExt> productMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (CollectionsUtil.isNotEmpty(paasFormDataESList)) {
            paasFormDataESList.forEach(item -> productMap.put(item.getId(), item));
        }
        // 限制返回值
        List<String> fieldList = Arrays.asList(ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.PRODUCT_ID), ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.PARENT_ID), ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.NUM), ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.WAREHOUSE_NAME), ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.WAREHOUSE_ID), ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.COST));
        List<PaasFormDataEntity> productWarehouseEntityList = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE,boolQueryBuilder,PaasFormDataEntity.class,fieldList);
        List<ProductWarehouseEntity> productWarehouseEntities = transferSubFormHelper.transferFormDataToProductWarehouseList(productWarehouseEntityList);
        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID,corpid);
        param.put(StringConstant.DEL,DelEnum.NORMAL.getDel());
        //先取出要遍历的warehouseIds
        List<Long> warehouseIds = new ArrayList<>();
        productWarehouseEntities.forEach(item ->{warehouseIds.add(item.getWarehouseId());});
        if (Objects.nonNull(warehouseIds) && warehouseIds.size()>0){
            param.put("idIn", warehouseIds);
        }
        List<WarehouseEntityExt> warehouseEntityExtList = warehouseModel.findEntitys(param);
        Map<Long, String> warehouseNameMap = new HashMap<>(warehouseEntityExtList.size());
        for (WarehouseEntityExt warehouseEntityExt : warehouseEntityExtList) {
            if (Objects.nonNull(warehouseEntityExt.getData()) && StringUtils.isNotBlank(warehouseEntityExt.getData().getString(WarehouseEnum.WAREHOUSE_NAME.getAttr()))){
                warehouseNameMap.put(warehouseEntityExt.getId(), warehouseEntityExt.getData().getString(WarehouseEnum.WAREHOUSE_NAME.getAttr()));
            }
        }
        Map<Long,JSONArray> productWarehouseMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (ProductWarehouseEntity entity : productWarehouseEntities){
            Long warehouseId = entity.getWarehouseId();
            entity.setWarehouseName(warehouseNameMap.get(warehouseId));
            if ((warehouseIdList.isEmpty() && !Objects.equals(warehouseId, 0L)) || warehouseIdList.contains(warehouseId)) {
                JSONObject singleWarehouse = new JSONObject();
                Long productId = entity.getProductId();
                boolean isCostFieldNeedHide = false;
                boolean isStockFieldNeedHide = false;
                try {
                    PaasFormDataEntityExt product = productMap.get(productId);
                    JSONObject data = product.getData();
                    Long parentId = data.getLong(ProductEnum.PARENT_ID.getAttr());
                    HandlerExplainDTO handlerExplainDTO = new HandlerExplainDTO();
                    BeanUtil.copyProperties(formDataListDTO, handlerExplainDTO);
                    handlerExplainDTO.setCreatorId(product.getCreatorId());
                    handlerExplainDTO.setOwnerId(mainUserMap.getOrDefault(parentId, new ArrayList<>()));
                    handlerExplainDTO.setCoUserId(coUserMap.getOrDefault(parentId, new ArrayList<>()));
                    handlerExplainDTO.setExplainList(fieldAttrEntityList);
                    isCostFieldNeedHide = ExplainUtil.isFieldNeedHide(costField, handlerExplainDTO) || Objects.equals(costField.getIsOpen(), 0);
                    isStockFieldNeedHide = ExplainUtil.isFieldNeedHide(stockField, handlerExplainDTO) || Objects.equals(stockField.getIsOpen(), 0);
                } catch (Exception e) {
                    LOG.info("ProductListHelp.childProductList", e);
                }
                JSONArray childProductArray = productWarehouseMap.getOrDefault(productId,new JSONArray());
                singleWarehouse.put("warehouse", entity.getWarehouseName());
                double cost = entity.getCost() == null ? 0D : entity.getCost();
                double stock = entity.getNum() == null ? 0D : entity.getNum();
                singleWarehouse.put("productId",productId);
                if (isCostFieldNeedHide) {
                    singleWarehouse.put("cost", BasicConstant.NULL_SHOW_STR);
                } else {
                    singleWarehouse.put("cost",  attrDecimalPrecisionHelper.setPrecision(cost, costAccuracy));
                }
                if (isStockFieldNeedHide) {
                    singleWarehouse.put("stock", BasicConstant.NULL_SHOW_STR);
                } else {
                    singleWarehouse.put("stock", stock);
                }
                if (!isCostFieldNeedHide && !isStockFieldNeedHide) {
                    singleWarehouse.put("costTotal", attrDecimalPrecisionHelper.setPrecision(cost*stock, costAccuracy));
                } else {
                    singleWarehouse.put("costTotal", BasicConstant.NULL_SHOW_STR);
                }
                childProductArray.add(singleWarehouse);
                productWarehouseMap.put(productId, childProductArray);
            }
        }
        return productWarehouseMap;
    }

    public Map<Long,String> categoryList(String corpid) {
        return productCategoryModel.categoryList(corpid);
    }

    /**
     * 金额设置
     * @param data
     * @param value
     */
    public void setParentPrice(JSONObject data, JSONArray value, FieldAttrEntity fieldAttrEntity) throws XbbException {
        String attr = fieldAttrEntity.getAttr();
        String saasAttr = fieldAttrEntity.getSaasAttr();
        Integer accuracy = fieldAttrEntity.getAccuracy();
        boolean amountFlag = Objects.equals(fieldAttrEntity.getAmountFlag(), 1);
        if (Objects.isNull(accuracy)) {
            accuracy = AttrDecimalPrecisionHelper.DEFAULT_PRECISION;
        }
        JSONObject minProduct = (JSONObject) value.stream().min(Comparator.comparing(item -> ((JSONObject)item).getDouble(saasAttr))).orElse(null);
        Double lowPriceStr = 0D;
        if (minProduct != null) {
            lowPriceStr = StringUtil.toDouble(minProduct.getString(saasAttr), 0D);
        }
        JSONObject maxProduct = (JSONObject) value.stream().max(Comparator.comparing(item -> ((JSONObject)item).getDouble(saasAttr))).orElse(null);
        Double highPriceStr = 0D;
        if (maxProduct != null) {
            highPriceStr = StringUtil.toDouble(maxProduct.getString(saasAttr), 0D);
        }
        if (Objects.equals(Double.compare(lowPriceStr, highPriceStr), 0)) {
            if (amountFlag) {
                data.put(attr, AttrDecimalPrecisionHelper.parseThousandth(lowPriceStr, accuracy));
            } else {
                data.put(attr, attrDecimalPrecisionHelper.setPrecision(lowPriceStr, accuracy));
            }
        } else {
            String value4Show;
            if (amountFlag) {
                value4Show = String.format(StringConstant.PRICE_RANFE, AttrDecimalPrecisionHelper.parseThousandth(lowPriceStr, accuracy), AttrDecimalPrecisionHelper.parseThousandth(highPriceStr, accuracy));
            } else {
                value4Show = String.format(StringConstant.PRICE_RANFE, AttrDecimalPrecisionHelper.setPrecisionStr(lowPriceStr.toString(), accuracy), AttrDecimalPrecisionHelper.setPrecisionStr(highPriceStr.toString(), accuracy));
            }
            data.put(attr, value4Show);
        }
    }


    public void setProductData(PaasFormDataEsListVO formDataListVO, ProductListDataDTO productListDataDTO) throws XbbException {
        List<Long> dataIdList = new ArrayList<>();
        formDataListVO.getPaasFormDataESList().forEach(item -> dataIdList.add(item.getDataId()));
        String corpid = productListDataDTO.getCorpid();
        List<Long> warehouseIdList = warehouseService.getWarehouseIdsByPermission(productListDataDTO.getLoginUser(), productListDataDTO.getLoginUser().getDepSet());
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        boolQueryBuilder.filter(termsQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.PRODUCT_ID), dataIdList));
        boolQueryBuilder.filter(termQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.WAREHOUSE_CHECKED), 1));
        boolQueryBuilder.filter(termsQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.WAREHOUSE_ID), warehouseIdList));
        List<PaasFormDataEntity> productWarehouseEntityList = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE, boolQueryBuilder, PaasFormDataEntity.class, Arrays.asList("id", ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.PRODUCT_ID), ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.WAREHOUSE_NAME), ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.NUM), ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.COST)));
        List<ProductWarehouseEntity> productWarehouseEntities = transferSubFormHelper.transferFormDataToProductWarehouseList(productWarehouseEntityList);
        Map<Long, JSONArray> warehouseMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (ProductWarehouseEntity entity : productWarehouseEntities) {
            JSONArray jsonArray = warehouseMap.getOrDefault(entity.getProductId(), new JSONArray());
            JSONObject json = new JSONObject();
            json.put("id", entity.getId());
            json.put("num", entity.getNum());
            json.put("cost", entity.getCost());
            json.put("value", entity.getWarehouseName());
            jsonArray.add(json);
            warehouseMap.put(entity.getProductId(), jsonArray);
        }
        for (PaasFormDataEntityExt entityExt : formDataListVO.getPaasFormDataESList()) {
            JSONObject data = entityExt.getData();
            JSONObject productData = new JSONObject();
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                String productAttr = "";
                if (!productAttr.isEmpty()) {
                    productData.put(productAttr, data.get(entry.getKey()));
                }
            }
            productData.put("warehouseArray", warehouseMap.getOrDefault(entityExt.getDataId(), new JSONArray()));
            entityExt.setProductData(productData);
        }
    }

    public List<Long> extractWarehouse(JSONObject commonFilter) {
        if (commonFilter.containsKey("warehouseId")){
            Long warehouseId = commonFilter.getLong("warehouseId");
            if (Objects.nonNull(warehouseId)){
                return Collections.singletonList(warehouseId);
            }
        }
        return new ArrayList<>();

    }

    /**
     * 产品供应商
     * @param data
     * @param supplierProductList
     */
    public void setProductSupplier(JSONObject data, List<SupplierProductEntity> supplierProductList, FieldAttrEntity fieldAttrEntity) throws XbbException {
        if (Objects.nonNull(supplierProductList)) {
            Set<Long> supplierIdSet = new HashSet<>();
            JSONArray jsonArray = new JSONArray();
            for (SupplierProductEntity supplierProductEntity : supplierProductList) {
                Long supplierId = supplierProductEntity.getSupplierId();
                if (supplierIdSet.contains(supplierId)) {
                    continue;
                }
                supplierIdSet.add(supplierId);
                JSONObject linkObject = new JSONObject();
                linkObject.put(StringConstant.SAAS_LINK_BUSINESS_ID, supplierId);
                linkObject.put(StringConstant.SAAS_LINK_BUSINESS_NAME, supplierProductEntity.getSupplierName());
                linkObject.put(StringConstant.SAAS_LINK_BUSINESS_LINK_TYPE,XbbRefTypeEnum.SUPPLIER.getCode());
                linkObject.put("saasMark", SaasMarkEnum.SAAS.getCode());
                jsonArray.add(linkObject);
            }
            data.put(ProductEnum.SUPPLIER.getAttr(), jsonArray);
        }
    }
}
